class PagesController < ApplicationController
	before_filter :find_page, :only=>[:show, :destroy, :edit, :update]
  before_filter :login_required, :except=>[:show]
    
  def index
    #@pages = Page.paginate(:page => params[:page], :per_page => 15, :order=>"created_at DESC")
    @pages = Page.find(:all)
  end
  
  def new
  	@page = Page.new
  end
  
  def create
    @page = current_user.pages.create!(params[:page])
    #@page.create_photo(params[:photo]) unless params[:photo][:uploaded_data].blank?
    redirect
    rescue ActiveRecord::RecordInvalid
    	render :action => 'new'
  end
    
  def update
    @page.update_attributes!(params[:page])
    
    #unless params[:photo][:uploaded_data].blank?
    #	@page.photo.nil? ? @page.create_photo(params[:photo]) : @page.photo.update_attributes(params[:photo])
    #end
    redirect
    rescue ActiveRecord::RecordInvalid
    	render :action => 'edit'
  end
  
  def destroy
    @page.destroy
    redirect
  end
  
  private
  
  def authorized?
    logged_in? && current_user.admin?
  end
  
  def find_page
    @page = Page.find_by_slug(params[:id])
    render_404 unless @page
  end
  
  def redirect
		redirect_to pages_path
  end
end
